export default class Play {
  constructor(scene, x, y, img) {
    console.log(scene)
    this.scene = scene
    // 玩家移动速度
    this.speed = 8
    // 玩家是否无敌
    this.isInvincible = false
    // 玩家等级
    this.powerLevel = 1

    //无敌状态动画
    this.shield = null

    // //玩家组
    // this.players = this.scene.physics.add.group() //创建玩家组

    //玩家子弹组
    this.myFires = this.scene.physics.add.group() //创建子弹组

    this.isFirstKeyDown = true

    //监听键盘事件
    this.cursors = this.scene.input.keyboard.addKeys('W,A,S,D,space,up,down,left,right,J')
  }

  //创建玩家
  create_play(x, y, key, index) {
    this.sprite = this.scene.physics.add.sprite(x, y, key, 0)
    this.sprite.setCollideWorldBounds(true)
    //添加标识
    this.sprite.setData('play', index) //设置玩家标识
    this.sprite.setData('isPlayer', true) //是否为玩家

    //添加玩家属性
    this.sprite.setData('isInvincible', false) //是否无敌
    this.sprite.setData('powerLevel', 0) //玩家等级
    this.sprite.setData('timeToFire', 1000) //子弹攻速
    this.sprite.setData('facing', 0) //玩家朝向

    //添加玩家动画
    this.update_animation(key, 0)

    //前5秒无敌
    this.makeInvincible(this.sprite)
    
    return this.sprite
  }

  //获取玩家子弹组
  getMyFires() {
    return this.myFires
  }



  //玩家动画管理
  update_animation(key, type) {
    //判断是否存在动画，存在则删除
    if (this.sprite.anims.currentAnim) {
      this.remove_animation()
    }
    this.sprite.setTexture(key, type)
    this.sprite.anims.create({
      key: 'up',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 0, end: type + 1 }),
      frameRate: 5,
      repeat: -1
    })

    this.sprite.anims.create({
      key: 'right',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 8, end: type + 9 }),
      frameRate: 5,
      repeat: -1
    })

    this.sprite.anims.create({
      key: 'down',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 16, end: type + 17 }),
      frameRate: 5,
      repeat: -1
    })

    this.sprite.anims.create({
      key: 'left',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 24, end: type + 25 }),
      frameRate: 5,
      repeat: -1
    })
  }

  //删除玩家动画
  remove_animation() {
    this.sprite.anims.remove('up')
    this.sprite.anims.remove('right')
    this.sprite.anims.remove('down')
    this.sprite.anims.remove('left')
  }

  //玩家发射子弹
  actKeyDown() {
    if (this.scene.isOver || !this.sprite.active || (!this.cursors.space.isDown && this.sprite.getData('play') == 1)) {
      return
    }
    if (this.scene.isOver || !this.sprite.active || (!this.cursors['J'].isDown && this.sprite.getData('play') == 2)) {
      return
    }
    // 控制子弹发射间隔
    if (this.isFirstKeyDown || this.scene.time.now >= this.sprite.timeToFire) {
      this.sprite.timeToFire = this.scene.time.now + this.sprite.getData('timeToFire')
      this.fireBullet()
      // 第一次按下后将 isFirstKeyDown 设置为 false
      if (this.isFirstKeyDown) {
        this.isFirstKeyDown = false
      }
    }
  }

  //发射子弹
  fireBullet() {
    this.scene.soundFire.play()
    // 获取玩家当前的位置和朝向
    const playerX = this.sprite.x
    const playerY = this.sprite.y
    const facing = this.sprite.getData('facing') //获取玩家的朝向

    // 计算子弹的初始位置，这里假设子弹在玩家前方一定距离的位置
    let fireX, fireY
    if (facing === 0) {
      // 上
      fireX = playerX
      fireY = playerY - 10 // 根据需要的距离来调整
    } else if (facing === 1) {
      // 右
      fireX = playerX + 10 // 根据需要的距离来调整
      fireY = playerY
    } else if (facing === 2) {
      // 下
      fireX = playerX
      fireY = playerY + 10 // 根据需要的距离来调整
    } else if (facing === 3) {
      // 左
      fireX = playerX - 10 // 根据需要的距离来调整
      fireY = playerY
    }
    console.log(fireX, fireY)
    // 创建子弹
    const fire = this.myFires.get(fireX, fireY, 'bullet', facing) // 假设 "bullet" 是子弹的纹理
    fire.setData('type', this.powerLevel)

    console.log(fire)
    if (fire) {
      // 设置子弹的速度，根据玩家的朝向来确定
      if (facing === 0) {
        // 上
        fire.setVelocity(0, -300) // 根据需要的速度来调整
      } else if (facing === 1) {
        // 右
        fire.setVelocity(300, 0) // 根据需要的速度来调整
      } else if (facing === 2) {
        // 下
        fire.setVelocity(0, 300) // 根据需要的速度来调整
      } else if (facing === 3) {
        // 左
        fire.setVelocity(-300, 0) // 根据需要的速度来调整
      }
    }
  }

  // 玩家移动
  update_move() {
    if (this.sprite.active) {
      let xx = 0
      let yy = 0
      if (this.sprite.getData('play') === 2) {
        if (this.cursors.up.isDown) {
          yy = -this.speed
          this.sprite.anims.play('up', true)
          this.sprite.setData('facing', 0)
        } else if (this.cursors.down.isDown) {
          yy = this.speed
          this.sprite.anims.play('down', true)
          this.sprite.setData('facing', 2)
        } else if (this.cursors.left.isDown) {
          xx = -this.speed
          this.sprite.anims.play('left', true)
          this.sprite.setData('facing', 3)
        } else if (this.cursors.right.isDown) {
          xx = this.speed
          this.sprite.anims.play('right', true)
          this.sprite.setData('facing', 1)
        } else {
          this.sprite.anims.stop()
        }
      } else {
        //监听wasd键
        if (this.cursors.W.isDown) {
          yy = -this.speed
          this.sprite.anims.play('up', true)
          this.sprite.setData('facing', 0)
        } else if (this.cursors.S.isDown) {
          yy = this.speed
          this.sprite.anims.play('down', true)
          this.sprite.setData('facing', 2)
        } else if (this.cursors.A.isDown) {
          xx = -this.speed
          this.sprite.anims.play('left', true)
          this.sprite.setData('facing', 3)
        } else if (this.cursors.D.isDown) {
          xx = this.speed
          this.sprite.anims.play('right', true)
          this.sprite.setData('facing', 1)
        } else {
          this.sprite.anims.stop()
        }
      }
      this.move(xx, yy)
    } else {
      console.log('玩家已死亡')
    }
  }

  move(xx, yy) {
    this.sprite.x = yy !== 0 ? Phaser.Math.RoundTo(this.sprite.x / 8) * 8 : this.sprite.x
    this.sprite.y = xx !== 0 ? Phaser.Math.RoundTo(this.sprite.y / 8) * 8 : this.sprite.y
    this.sprite.setVelocity(xx * this.speed, yy * this.speed)

    if (this.shield) {
      this.shield.x = this.sprite.x
      this.shield.y = this.sprite.y
    }
  }

  //玩家升级
  upgrade() {
    this.powerLevel++
    this.sprite.setData('powerLevel', this.powerLevel)
    this.sprite.setData('timeToFire', this.sprite.getData('timeToFire') - 100)

    if (this.powerLevel > 3) {
      return
    }
    console.log('当前玩家等级：' + this.powerLevel)
    //通过玩家等级计算初始帧

    let type = Math.floor(this.powerLevel / 4) * 32 + (this.powerLevel % 4) * 2

    //更换玩家的纹理、动画
    this.update_animation('tank', type)

    //玩家移速增加
    this.speed += 2
  }

  //玩家无敌状态
  makeInvincible(player) {
    console.log('无敌');
    if (this.shield) {
      this.shield.destroy()
    }
    //在玩家实体上添加一个无敌状态的动画且跟随玩家
    this.shield = this.scene.add.sprite(player.x, player.y, 'shield')
    // 添加玩家的动画
    this.scene.anims.create({
      key: 'playerInvincible',
      frames: player.anims.generateFrameNumbers('shield', { start: 0, end: 2 }),
      frameRate: 10,
      repeat: -1
    })
    this.shield.anims.play('playerInvincible', true)
    player.setData('isInvincible', true)

    //设置无敌状态时间
    setTimeout(() => {
      this.shield.destroy()
      player.setData('isInvincible', false)
    }, 10000)
  }
}
